// TOWN SCRIPT
//    Town 4: Arena

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 4, 1 = won at least 15 fights
// 4, 2 = number of fights won (1 - 15, then resets)
// 4, 3 = monster # currently fighting in the arena
// 4, 16 = combat warning flag

begintownscript;

variables;

short mons, ii, fts, itm, plyr, plvl, mlvl, nmlvl, choice;
string w_str = "worms";
string c_str = "champions";
string firstline;

body;

beginstate INIT_STATE;
	set_crime_tolerance(6);

	set_name(11, "Marjorie");
	set_char_dialogue_pic(11, 527, 0);
	set_name(12, "John Smith");
	set_char_dialogue_pic(12, 524, 0);
	set_name(13, "Father Quick");
	set_char_dialogue_pic(13, 1949, 0);
	set_name(14, "Barbarous");
	set_char_dialogue_pic(14, 1960, 0);
	set_name(15, "Jim");
	set_char_dialogue_pic(15, 1955, 0);
	set_name(22, "Mary");
	set_char_dialogue_pic(22, 1959, 0);
	set_name(23, "Susan");
	set_char_dialogue_pic(23, 530, 0);
	set_name(24, "Jamie");
	set_char_dialogue_pic(24, 528, 0);
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
	if (get_ran(1, 0, 100) < 6) {
		if (get_ran(1, 1, 100) < 50)
			text_bubble_on_char(6, "Don't cause any trouble!");
		else
			text_bubble_on_char(6, "Do your fighting in the arena!");
	}
	if (get_ran(1, 0, 100) < 6) {
		if (get_ran(1, 1, 100) < 50)
			text_bubble_on_char(7, "Do your fighting in the arena!");
		else
			text_bubble_on_char(7, "Don't cause any trouble!");
	}
	// do checks if in arena combat
	if (get_flag(4, 13) > 0) {
		if ((is_combat()) && (get_flag(4, 16) == 0)) {
			set_flag(4, 16, 1);
			message_dialog("A voice booms out _Warning, cannot guarantee survivability while in combat mode_.", "");
		}
		plyr = get_selected_char();
		// check if player in trouble in arena - loses automatically
		if ((char_status(plyr) != 1) || (get_health(plyr) < 15)) {
			// player has lost - rejoin party, then revive
			reunite_party();
			restore_pc(plyr);
			play_sound(27);
			message_dialog("You have lost your fight and have been teleported back to your companions.", "The victor is still getting crowd acclamation within the arena");
			kill_char(get_flag(4, 3), 2, 0);
			set_flag(4, 3, 0);
			set_flag(4, 13, 0);
		}
	}
break;

beginstate 10;
	if (get_flag(4, 0) == 0) {
		play_sound(0);
		message_dialog("You step into the lengendary Arena where people have gone for years seeking fortune.", "You go forward in anticipation with the sounds of battles echoing among the walls and the expectation of fame and glory.");
		set_flag(4, 0, 1);
	}
break;

beginstate 11;
	set_flag(4, 13, 0);
	clear_buffer();
	append_string("A man behind the window looks up at you.  _Welcome to the Arena ");
	if (get_flag(4, 1) == 0)
		append_string(w_str);
	else
		append_string(c_str);
	append_string("._");
	get_buffer_text(firstline);
	reset_dialog();
	add_dialog_str(0, firstline, 0);
	add_dialog_str(1, "_The rules are simple, 150 coins will get one of your party into the arena to face a random opponent._", 0);
	add_dialog_str(2, "_You can stay and fight or run like a little chicken._", 0);
	add_dialog_str(3, "_In either case, you will exit in the rear of the arena._", 0);
	add_dialog_str(4, "_I do not have all day, what do you want to do?_", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Have party member enter arena");
	choice = run_dialog(1);
	if (choice == 2) {
		if (pay_coins(150) > 0) {
			play_sound(38);
			if (try_to_split_party(33, 24, 1) > 0) {
				block_entry(1);
				end();
			}
			else {
				message_dialog("After failing to pick who would go into the arena and fight, the man hands back your coins.", "He turns to help another, less hesitant soul.");
				change_coins(150);
			}
		}
		else {
			message_dialog("_You do not have enough coins for the entrance fee._", "_Begone you curs._  the man says before turning away.");
		}
	}
	else {
		message_dialog("The man chuckles and turns away to help another brave soul.", "");
	}
	block_entry(0);
break;

beginstate 12;
	if (get_flag(4, 13) > 0)
		end();
	mons = get_flag(4, 3);
	if (mons > 0) {
		// clear monster if already there
		kill_char(mons, 2, 0);
	}
	// roll new monster and set into flag field (L20 - L30 creatures)
	ii = get_ran(1, 1, 20);
	if (ii == 1)
		mons = 16;
	else if (ii == 2)
		mons = 20;
	else if (ii == 3)
		mons = 33;
	else if (ii == 4)
		mons = 41;
	else if (ii == 5)
		mons = 58;
	else if (ii == 6)
		mons = 60;
	else if (ii == 7)
		mons = 87;
	else if (ii == 8)
		mons = 94;
	else if (ii == 9)
		mons = 95;
	else if (ii == 10)
		mons = 97;
	else if (ii == 11)
		mons = 100;
	else if (ii == 12)
		mons = 103;
	else if (ii == 13)
		mons = 106;
	else if (ii == 14)
		mons = 122;
	else if (ii == 15)
		mons = 126;
	else if (ii == 16)
		mons = 127;
	else if (ii == 17)
		mons = 163;
	else if (ii == 18)
		mons = 174;
	else if (ii == 19)
		mons = 184;
	else if (ii == 20)
		mons = 187;
	// place it in the arena - not worth xp or items...
	place_monster(19, 20, mons, 1);
	mons = char_on_spot(19, 20);
	if (get_attitude(mons) < 10)
		set_attitude(mons, 10);
	// set courage/agression to max for combatant
	set_aggression(mons, 100);
	set_courage(mons, 100);
	// balance the battle somewhat	
	plyr = get_selected_char();
	plvl = get_level(plyr);
	mlvl = get_level(mons);
	nmlvl = mlvl;
	if (plvl <= 15)
		nmlvl = mlvl - 10;
	else if (plvl >= 30)
		nmlvl = mlvl + 10;
	if (nmlvl != mlvl)
		set_level(mons, nmlvl);
	set_flag(4, 3, mons);
	set_flag(4, 13, 1);
	set_flag(4, 16, 0);
	play_sound(57);
	print_str_color("The crowd cheers as your opponent enters and awaits in the arena.", 2);
break;

beginstate 13;
	block_entry(1);
	if (is_combat()) {
		message_dialog("A man behind the gate prevents you from leaving.", "_Sheathe your weapon first._ he says (get out of combat mode!)");
		end();
	}
	reunite_party();
	// clear monster (if not dead)
	mons = get_flag(4, 3);
	set_flag(4, 3, 0);
	set_flag(4, 13, 0);
	if (mons > 0) {
		if (char_ok(mons) == 0) {
			// its dead
			print_str_color("The cheers of the crowd still ring as you rejoin your party.", 3);
			inc_flag(4, 2, 1);
			if (get_flag(4, 2) == 15) {
				set_flag(4, 1, 1);
				set_flag(4, 2, 0);
				// give choice of prize
				play_sound(26);
				reset_dialog();
				add_dialog_str(0, "Congratulations, you have won enough fights to get a prize.", 0);
				add_dialog_str(1, "You can choose a sword, bow, helm, shield or ring.", 0);
				add_dialog_str(2, "Pick well champion.", 0);
				add_dialog_choice(0, "Pick");
				choice = run_dialog(0);
				itm = 0;
				while (itm == 0) {
					get_text_response("Which prize (sword, bow, helm, shield or ring)?");
					check_text_response_match("sword");
					if (got_text_match() > 0)
						itm = 453;
					check_text_response_match("bow");
					if (got_text_match() > 0)
						itm = 454;
					check_text_response_match("helm");
					if (got_text_match() > 0)
						itm = 455;
					check_text_response_match("shield");
					if (got_text_match() > 0)
						itm = 456;
					check_text_response_match("ring");
					if (got_text_match() > 0)
						itm = 432;
					if (itm == 0)
						message_dialog("Enter sword, bow, helm, shield or ring.", "");
				}
				if (reward_give(itm) == 0) {
					play_sound(0);
					print_str_color("Could not give prize, giving XP and coins instead.", 4);
					award_party_xp(100, 40);
					change_coins(300);
				}
			}
		}
		else {
			kill_char(mons, 2, 0);
			print_str_color("Boos of the crowd still echo as you rejoin your party.", 3);
		}
	}
break;

beginstate 14;
	if (get_flag(4, 4) == 0) {
		set_flag(4, 4, 1);
		message_dialog("This appears to be the arena spectator area.", "The windows to the north face over the vast arena floor.");
	}
break;

beginstate 15;
	if (get_flag(4, 5) == 0) {
		set_flag(4, 5, 1);
		message_dialog("This room appears to be for spectator overflow.", "Not quite as nice as the main room, but still gives a good view of the fights.");
	}
break;

beginstate 16;
	reset_dialog();
	add_dialog_str(0, "A small collection plate adorns the top of the pedestal.", 0);
	add_dialog_str(1, "A sign at the bottom says _Blessed are those that give._", 0);
	add_dialog_str(2, "Do you:", 0);
	add_dialog_choice(0, "Throw in a few coins (5)");
	add_dialog_choice(1, "Leave");
	choice = run_dialog(1);
	if (choice == 1) {
		if (pay_coins(5) > 0) {
			play_sound(38);
			inc_flag(4, 11, 1);
			if (get_flag(4, 11) == 5) {
				set_flag(4, 11, 0);
				message_dialog("The priest thanks you and then bestows a blessing.",
					"You all feel better.");
				play_sound(166);
				revive_party();
			}
			else {
				message_dialog("The priest thanks you for your donation.",
					"You feel if you give more something good will happen.");
			}
		}
	}
break;

beginstate 17;
	if (get_flag(4, 12) == 0) {
		set_flag(4, 12, 1);
		reset_dialog();
		add_dialog_str(0, "You enter the quiet sanctuary of the warriors temple.", 0);
		add_dialog_str(1, "A priest is busily tending to souls who have wandered in from the arena fights.", 0);
		add_dialog_str(2, "You feel at peace.", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(1);
	}
break;
